<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/jApiController.php';

class UserModel extends jApiController{
	public function def(){
	  return 'welcome to User jApi!';
	}
	
	public static function getView(){
	  include_once $_SERVER['DOCUMENT_ROOT'].'/lib/UserView.php';
	  return UserView::inst();
	}

	public function getUser($id=false){
	  if( empty($id) ) //throw_error('get user: ID expected',400);
	  	return $this->error('get user: ID expected');

	  if(!$this->checkAuth()) //throw_error('Auth expected',400);
	    return $this->error('Auth expected');
	  if( substr($id, 0, 2)=='ur' )
			$where=' u_id=:id';
		else
			$where=' u_url=:id';
		$sql="SELECT	u_id,u_type,u_name,u_nameshort,u_createtime,u_email
					FROM		user
					WHERE		$where";

	  $rs=$this->getData(
			$sql,
			array('id'=>$id),
			true
		);
		return $rs;
	}
	
	public function getUserByToken($id=false){
	  if( empty($id) ) //throw_error('token: ID expected',400);
	    return $this->error('token: ID expected');
	  if( substr($id, 0, 2)!='tn' )	//throw_error('token: only token ID expected',400);
	  	return $this->error('token: only token ID expected');

	  return $this->getData(
			"SELECT	u_id,u_type,u_name,u_nameshort,u_createtime,u_email
			FROM		user,token
			WHERE		t_id=:id AND t_dead > :now AND u_id=t_key_user",
			array('id'=>$id, 'now'=>mktime()),
			true
		);
	}
	
	public function getUserByEmail($id=false){
	  if( empty($id) ) //throw_error('byEmail: email expected',400);
	    return $this->error('byEmail: email expected');
	  if( !preg_match('/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$/i', $id) )
			//throw_error('byEmail: only email expected',400);
			return $this->error('byEmail: email expected');
		$sql="SELECT	u_id,u_type,u_name,u_nameshort,u_createtime,u_email
					FROM		user LEFT JOIN user_soc ON(u_id=us_key_user)
					WHERE		us_email=:id OR u_email=:id2";
	  return $this->getData(
			$sql,
			array('id'=>$id,'id2'=>$id),
			true
		);
	}
	
	public function checkToken($id=false){
	  if( empty($id) ) //throw_error('token: ID expected',401 );
	    return $this->error('checkToken: ID expected');
	  if( substr($id, 0, 2)!='tn' )	//throw_error('token: only token ID expected',401 );
	    return $this->error('checkToken: only token ID expected');
	  $rs=$this->getData(
			"SELECT	t_id,t_key_user,t_dead
			FROM		token
			WHERE		t_id=:id",
			array('id'=>$id),
   		true
		);
		
		if(empty( $rs )) 
		  return $this->error('checkToken: token not found');
		elseif($rs['t_dead'] <= mktime()) //throw_error('token expired',404);
		  return $this->error('checkToken: token expired');
		return $rs;
	}

	
	public function getUsersByGeo($id){
	  if( empty($id) ) //throw_error('user by geo: ID expected',400);
	    return $this->error('user by geo: ID expected');
	  if(!$this->maskID($id,'country|region|city|urban')) //throw_error('user by geo: expected ID of country,region,city or urbanarea',400);
	    return $this->error('user by geo: expected ID of country,region,city or urbanarea');
	  //Получение GET параметров
	  $limit=20;
	  if($prm['page']=='all') $limit=100;
   	$page=intval($prm['page']); if(empty($page))$page=1;
    $page--;

		$sql="SELECT DISTINCT u_id,u_type,u_name,u_name,u_nameshort
			 FROM user,user2geo
			 WHERE u2g_key_user=u_id AND u2g_key_geo=:id";
			 
		$sql_prm=array( 'id'=>$id );
			 
		$sql.=' LIMIT :from,:limit';
		$sql_prm['from']	=array(	($page*$limit),PDO::PARAM_INT	);
		$sql_prm['limit']=array(	$limit,PDO::PARAM_INT	);
		$result=$this->getData($sql,$sql_prm);

		return $this->genListResult(
		  array(
			  'total'=>(int)$total['ttl'],
			  'page'=>$page+1,
			  'limit'=>$limit,
			),
			$result
		);
	  //return 
	}
}
?>